package com.hk.service.impl;

import com.hk.dao.CompanyProfileDao;
import com.hk.model.CompanyProfile;
import com.hk.model.Document;
import com.hk.service.ICompanyProfileService;
import com.hk.utils.DateUtils;
import com.hk.utils.Io;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: KangWang
 * Date: 2021/5/18 18:08
 * State：公司简介业务逻辑层
 */
public class CompanyProfileServiceImpl implements ICompanyProfileService {

    @Override
    public CompanyProfile findAllCompanyProfile(String path) {
        CompanyProfileDao companyProfileDao = new CompanyProfileDao();
        CompanyProfile companyProfile = companyProfileDao.findAllCompanyProfile();
        //      1、存储路径  2、文件夹 3、文件
        Document document = new Document(path,"CompanyProfile", "CompanyProfile.txt");
        //    将公司简介对象存入集合
        List<CompanyProfile> companyProfiles = new ArrayList<>();
        companyProfiles.add(companyProfile);
        writeData(companyProfiles,document);
        companyProfiles = (List<CompanyProfile>)readData(document);
        companyProfile = companyProfiles.get(0);
        if (companyProfile == null) {
            System.out.println("无数据");
            return null;
        }
        return companyProfile;
    }

    /**
     * 读取数据
     * @param document
     * @return
     */
    @Override
    public List<?> readData(Document document) {
        //    根据该对象内多少属性进行转化划分
        final int count = 9;
        List<CompanyProfile> companyProfiles =  new ArrayList<>();
        List<String> list = Io.readData(document);
        for (int i = 0; i < list.size(); i=i+count) {
//            时间转换
            Date startTime = DateUtils.stringTime(list.get(i + 4));
            Date endTime = DateUtils.stringTime(list.get(i + 5));
            CompanyProfile companyProfile = new CompanyProfile(
                    Integer.parseInt(list.get(i)),
                    list.get(i+1),
                    list.get(i+2),
                    list.get(i+3),
                    startTime,
                    endTime,
                    list.get(i+6),
                    list.get(i+7),
                    list.get(i+8)
            );
            companyProfiles.add(companyProfile);
        }
        return companyProfiles;
    }

    /**
     * 写入数据
     * @param list
     * @param document
     */
    @Override
    public void writeData(List<?> list, Document document) {
        List<CompanyProfile> companyProfiles = (ArrayList<CompanyProfile>) list;
        List<String> list1 = new ArrayList<>();
        CompanyProfile companyProfile = companyProfiles.get(0);
        list1.add(String.valueOf(companyProfile.getCompanyId()));
        list1.add(companyProfile.getCompanyName());
        list1.add(companyProfile.getNameEnglish());
        list1.add(companyProfile.getCompanyPhone());
//        时间转换
        String startTime = DateUtils.timeString(companyProfile.getStartTime());
        String endTime = DateUtils.timeString(companyProfile.getEndTime());
        list1.add(startTime);
        list1.add(endTime);
        list1.add(companyProfile.getCompanyIdea());
        list1.add(companyProfile.getIdeaEnglish());
        list1.add(companyProfile.getProfile());
        Io.writeData(list1,document);
    }
}
